//
//  BiaoShiViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/12/27.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class BiaoShiViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    var biaoshiArray = [biaoshiModel]()
    
    
    var bigbiaoshiArray = [biaoshiModel]()
    var collection :UICollectionView!
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    var index = 1
    

    
    var ImageViewArray : [String] = ["app_hot_rec_1","app_hot_rec_2","app_hot_rec_1","app_hot_rec_2","app_hot_rec_4","app_hot_rec_3","app_hot_rec_1","app_hot_rec_2"]
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width:(self.view.frame.size.width - 20)/2,height:130)
        //列间距,行间距,偏移
        layout.minimumInteritemSpacing = 5
        layout.minimumLineSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5)
        
        
        if UIScreen.main.bounds.size.height == 812 {
            collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 44 - 83  - 64 - 20) , collectionViewLayout: layout)
        }else{
            collection = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height -  64 - 49) , collectionViewLayout: layout)
        }
        
        
        
        
        collection.backgroundColor = UIColor.white
        //        collection.register(FallCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "Cellid")
        
        collection.register(biaoshiCollectionViewCell.self, forCellWithReuseIdentifier: "Cellid")
        
        
        collection.delegate = self
        collection.dataSource = self
        self.view.addSubview(collection)
        
        
        
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        // 现在的版本要用mj_header
        collection.mj_header = header
        
        collection.mj_header.beginRefreshing()
        
        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        collection.mj_footer = footer
        
//        self.loadData()
    }
    
    @objc func headerRefresh(){
        self.biaoshiArray.removeAll()
        self.bigbiaoshiArray.removeAll()
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(20)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("666666====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = biaoshiModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    model1.articlePic = dataDic["articlePic"].string
                    
                    self.biaoshiArray.append(model1)
                    
                    
                    print("NextListArray====\(self.biaoshiArray.count)")
                    
                }
                self.bigbiaoshiArray.append(contentsOf: self.biaoshiArray)
                self.collection .reloadData()
                
                self.collection.mj_header.endRefreshing()
                
                
            }else{
                
                print("数据请求失败")
            }
            
        }
        
    }
    
    @objc func footerRefresh(){
        self.biaoshiArray.removeAll()
        
        index += 1
        
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(20)\",\"start\":\"\(index)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("666666====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = biaoshiModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    model1.articlePic = dataDic["articlePic"].string
                    
                    self.biaoshiArray.append(model1)
                    
                    
                    print("NextListArray====\(self.biaoshiArray.count)")
                    
                }
                self.bigbiaoshiArray.append(contentsOf: self.biaoshiArray)
                self.collection .reloadData()
                
                self.collection.mj_header.endRefreshing()
                
                
            }else{
                
                print("数据请求失败")
            }
            
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.biaoshiArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let biaoshi:biaoshiModel = self.biaoshiArray[indexPath.row]
        
        
        
        let cell : biaoshiCollectionViewCell = collection.dequeueReusableCell(withReuseIdentifier: "Cellid", for: indexPath) as! biaoshiCollectionViewCell
        
        
        
        cell.biaoshiWithModel(model: biaoshi)
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        print(indexPath.row)

        let biaoshi:biaoshiModel = self.biaoshiArray[indexPath.row]

        let WebVC =  WebViewController()
        WebVC.numInteger = biaoshi.id
        self.navigationController?.pushViewController(WebVC , animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func loadData(){
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(20)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("666666====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = biaoshiModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    model1.articlePic = dataDic["articlePic"].string
                    
                    self.biaoshiArray.append(model1)
                    
                    
                    print("NextListArray====\(self.biaoshiArray.count)")
                    
                }
                self.collection .reloadData()
                
                
            }else{
                
                print("数据请求失败")
            }
            
        }
        
    }
    
}


